<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="机构名称">
                <a-input v-model="queryParam.name" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => {
                  this.queryParam = {}
                  $refs.table.refresh(true)
                }">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新建</a-button>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :showPagination="false"
        :columns="columns"
        :data="loadData"
      >
        <span slot="status" slot-scope="text, record">
          <a-badge :status="record.status === '0' ? 'success' : 'default'" :text="record.status === '0' ? '显示' : '隐藏'" />
        </span>
        <span slot="icon" slot-scope="text, record">
          <a-icon v-if="record.icon" :type="record.icon" />
        </span>
        <span slot="tag" slot-scope="text, record">
          <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="created_at" slot-scope="text">
          <span>{{ text | dayjs }}</span>
        </span>
        <span slot="updated_at" slot-scope="text">
          <span>{{ text | dayjs }}</span>
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record, 1)" v-action:query>查看</a>
            <a-divider type="vertical" v-action:edit/>
            <a @click="handleEdit(record, 2)"  v-action:edit>修改</a>
            <a-divider type="vertical"  v-action:delete/>
            <a @click="handleEdit(record, 3)"  v-action:delete>删除</a>
          </template>
        </span>
      </s-table>

      <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { getList, add, edit, deletes } from '@/api/dept'

import CreateForm from './modules/CreateForm'

const columns = [
  {
    title: '机构名称',
    dataIndex: 'name'
  },
  {
    title: '机构编码',
    align: 'center',
    dataIndex: 'code',
  },
  {
    title: '排序',
    align: 'center',
    dataIndex: 'sort',
  },
  {
    title: '状态',
    align: 'center',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '创建时间',
    align: 'center',
    dataIndex: 'created_at',
    scopedSlots: { customRender: 'created_at' }
  },
  {
    title: '更新时间',
    align: 'center',
    dataIndex: 'updated_at',
    scopedSlots: { customRender: 'updated_at' }
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'Dept',
  components: {
    STable,
    Ellipsis,
    CreateForm,
  },
  data () {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getList(requestParameters)
          .then(res => {
            return {
              result: res.data
            }
          })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    handleAdd () {
      this.mdl = {
        disabled: false
      }
      this.visible = true
    },
    handleEdit (record, index) {
      record.type = record.type ? parseInt(record.type) : ''
      if (index === 1) {
        this.visible = true
        record.disabled = true;
        this.mdl = { ...record }
      } else if (index === 2) {
        this.visible = true
        record.disabled = false;
        this.mdl = { ...record }
      } else {
        this.confirmLoading = true
        deletes(record.id).then(ret => {
          this.visible = false
          this.confirmLoading = false
          // 刷新表格
          this.$refs.table.refresh()
          this.$message.info('删除成功')
        })
      }
    },
    handleOk (values) {
      const form = this.$refs.createModal
      this.confirmLoading = true
      if (values.id) {
        edit(values, values.id).then(res => {
          this.visible = false
          this.confirmLoading = false
          // 重置表单数据
          form.resetFields()
          if (res.success) {
            // 刷新表格
            this.$refs.table.refresh()
            this.$message.info('修改成功')
          } else {
            this.$message.info(ret.msg)
          }
        })
      } else {
        add(values).then(res => {
          this.visible = false
          this.confirmLoading = false
          // 重置表单数据
          form.resetFields()
         if (res.success) {
           // 刷新表格
           this.$refs.table.refresh()
           this.$message.info('新增成功')
         } else {
           this.$message.info(res.msg)
         }
        })
      }
    },
    handleCancel () {
      this.visible = false
      const form = this.$refs.createModal
      form.resetFields() // 清理表单数据（可不做）
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
